package v1

import (
	"Im/server/common/model"
	"fmt"

	"Im/server/system/middleware"

	"Im/server/util"

	"github.com/gin-gonic/gin"
)

type Login struct {
	ApiBaseNoVerify
}

// 登录
func (s Login) Login(c *gin.Context) {
	fmt.Println("333333333333333333333333333333333333333333")
	phone := c.DefaultPostForm("phone", "")

	if phone == "" {
		s.Error(0, "", "账号不能为空", c)
		return
	}
	passwd := c.DefaultPostForm("passwd", "")
	if passwd == "" {
		s.Error(0, "", "密码不能为空", c)
		return
	}
	uInfo := make(map[string]string)
	uInfo["phone"] = phone
	userModel := new(model.UserModel)
	reUserDetail, _, reExist := userModel.Detail(0, uInfo)
	if reExist == false {
		s.Error(0, "", "未注册, 请先注册", c)
		return
	}

	uInfo["salt"] = reUserDetail.Salt
	uInfo["passwd"] = passwd
	rePasswd := util.Passwd(0, uInfo)
	if reUserDetail.Passwd == rePasswd {

		uInfo["username"] = reUserDetail.Username
		token, bol := middleware.TokenEncode(util.Uint64ToStr(reUserDetail.Id), uInfo)
		if bol == false {
			s.Error(0, "", "token无效", c)
			return
		}
		var randstr string = util.RandStr()
		//通信唯一标识
		reUserDetail.Identify_comm = util.Md5(randstr + reUserDetail.Phone + util.Uint64ToStr(reUserDetail.Id) + token)
		reUserDetail.Passwd = ""
		reUserDetail.Salt = ""
		reUserDetail.Token = token
		reUserDetail.Lastloginip = ""
		reUserDetail.Lastlogintime = 0
	} else {
		s.Error(0, "", "账号或密码错误", c)
		return
	}
	s.Succee(reUserDetail, c)
	return
}

// 注册
func (s Login) Register(c *gin.Context) {

	fmt.Println("==================================================================")

	var userInfo = make(map[string]string)
	phone := c.DefaultPostForm("phone", "")
	if phone == "" {
		s.Error(0, "", "手机号不能为空", c)
		return
	}
	userInfo["phone"] = phone
	userModel := new(model.UserModel)
	_, _, reExist := userModel.Detail(0, userInfo)
	if reExist == true {
		s.Error(0, "", "已被注册", c)
		return
	}

	passwd := c.DefaultPostForm("passwd", "")
	if passwd == "" {
		s.Error(0, "", "密码不能为空", c)
		return
	}
	userInfo["passwd"] = passwd

	username := c.DefaultPostForm("username", "")
	if username == "" {
		userInfo["username"] = username
	} else {
		userInfo["username"] = ""
	}

	nickname := c.DefaultPostForm("nickname", "")
	if nickname == "" {
		userInfo["nickname"] = nickname
	} else {
		userInfo["realname"] = ""
	}

	realname := c.DefaultPostForm("realname", "")
	if realname != "" {
		userInfo["realname"] = realname
	} else {
		userInfo["realname"] = ""
	}

	sex := c.DefaultPostForm("sex", "")
	if sex != "" {
		userInfo["sex"] = sex
	} else {
		userInfo["sex"] = "0"
	}

	age := c.DefaultPostForm("age", "")
	if sex != "" {
		userInfo["age"] = age
	} else {
		userInfo["age"] = "0"
	}

	mail := c.DefaultPostForm("mail", "")
	if mail != "" {
		userInfo["mail"] = mail
	} else {
		userInfo["mail"] = ""
	}
	userInfo["lastloginip"] = c.ClientIP()
	userInfo["status"] = "0"

	result := userModel.Auserdd(userInfo)
	userInfo = nil
	if result == true {
		s.Succee("注册成功", c)
	} else {
		s.Error(0, "", "注册失败", c)
	}
}
